package com.ibeetl.admin.api.service;

import com.ibeetl.admin.api.dao.TravelAttractionsLineDao;
import com.ibeetl.admin.api.entity.*;
import com.ibeetl.admin.api.util.BeanToMap;
import com.ibeetl.admin.api.util.UrlUtils;
import com.ibeetl.admin.core.service.CoreBaseService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TravelAttractionsLineService  extends CoreBaseService<Travel_attractions_line> {
    @Resource
    private TravelAttractionsLineDao travelAttractionsLineDao;
    @Resource
    private TravelAttractionsService travelAttractionsService;
    @Resource
    private TravelPictureService travelPictureService;
    @Resource
    private TravelAudioService travelAudioService;
    @Resource
    private TravelVideoService travelVideoService;
    @Resource
    private RouteService routeService;


    @Value("${ip.address}")
    private String ipAddress;
    @Value("${server.port}")
    private String serverPort;

    public List<Travel_attractions_line> queryByUserId(Long userId) {
        List<Travel_attractions_line> list = travelAttractionsLineDao.queryByUserId(userId);
        return  list;
    }

    /**
     *
     * @param attractionId
     * @param flag  0 逻辑删除   1 物理删除
     */
    public void delete(Long attractionId ,int flag) {

        if(flag == 0){
            travelAttractionsLineDao.updateState(attractionId,3,new Date());
        }
        if(flag == 1){
            //查询所有景点的id
            List<Travel_attractions> list = travelAttractionsService.queryByAttractionId(attractionId);
            if(list != null && list.size()>0){
                for(Travel_attractions t : list){
                    // 删除景点路线记录，删除路线与景点关系，删除景点及文件
                    travelAttractionsService.delete(t.getAttsId(),1);
                }
            }
            //删除记录 坐标
            routeService.deleteByAttractionId(attractionId);
            //删除记录 路线
            travelAttractionsLineDao.deleteById(attractionId);
        }
    }

    public long queryNewIdByUserId(Integer user_id) {
        List<Long> list = travelAttractionsLineDao.queryNewIdByUserId(user_id);
        if(list == null || list.size()<1){
            return -1;
        }
        return list.get(0);
    }

    public boolean saveData(Travel_attractions_line travel_attractions_line) throws InvocationTargetException, IllegalAccessException {
        travel_attractions_line.setCreateDt(new Date());
        try{
            travelAttractionsLineDao.save(BeanToMap.beanToMap(travel_attractions_line));
            return true;
        }catch (Exception e){
            return false;
        }

//        return  this.save(travel_attractions_line);
    }

    public void updateData(Travel_attractions_line travel_attractions_line)  {

        travel_attractions_line.setUpdateDt(new Date());
        Map map = BeanToMap.beanToMap(travel_attractions_line);
        travelAttractionsLineDao.update(map);
//        this.update(travel_attractions_line);

    }


    public Travel_attractions_line queryById(Long id){
        return travelAttractionsLineDao.queryById(id);
    }

    public List<Travel_attractions_line> queryNoAudit() {
//        PageRequest pageRequest = pageParam.getPageRequest();
//        Map params = pageParam.getPageParam();
        return travelAttractionsLineDao.queryNoAudit();
    }

    public void audit(Travel_attractions_line travel_attractions_line) {
        travel_attractions_line.setReviewDt(new Date());
        // TODO 实际登录人
        travel_attractions_line.setReviewers(100000);
        Map map = BeanToMap.beanToMap(travel_attractions_line);
        travelAttractionsLineDao.audit(map);
    }

    public void addViewsNum(Long id){
        travelAttractionsLineDao.addViewsNum(id);

    }

    public void addLikeNum(Long id){
        travelAttractionsLineDao.addLikeNum(id);
    }


    public List<Travel_attractions_line> queryByState(Long userId, Integer state) {
        List<Travel_attractions_line> list = travelAttractionsLineDao.queryByState(userId,state);
        return  list;
    }

    public List<Travel_attractions_line_info> queryTravelAttractionsLineInfo(Long attractionId) {
        //查询景点下的图片、音频、视频
        //1、查询所有景点
        List<Travel_attractions>  travel_attractions_list  = travelAttractionsService.queryByAttractionId(attractionId);
        if(travel_attractions_list != null && travel_attractions_list.size()>0){
            List<Travel_attractions_line_info> list = new ArrayList<>();
            for(Travel_attractions travel_attractions : travel_attractions_list){


                Long attsId = travel_attractions.getAttsId();
                Long userId = travel_attractions.getUserId();
                //根据景点查询图片
                List<TravelPictureList>  travel_pictures  = travelPictureService.queryByAttsId(attsId,userId);
                if (travel_pictures != null && travel_pictures.size()>0){
                    for(TravelPictureList travelPictureList : travel_pictures){
                        Travel_attractions_line_info travel_attractions_line_info = get(travel_attractions,travelPictureList.getPictureAddress(),1);
                        list.add(travel_attractions_line_info);
                    }

                }
                //根据景点查询音频
                List<Travel_audio_list>  travel_audios  = travelAudioService.queryByAttsId(attsId,userId);
                if (travel_audios != null && travel_audios.size()>0){
                    for(Travel_audio_list travel_audio_list : travel_audios){
                        Travel_attractions_line_info travel_attractions_line_info = get(travel_attractions,travel_audio_list.getAudioAddress(),2);
                        list.add(travel_attractions_line_info);
                    }
                }
                //根据景点查询视频
                List<Travel_video_list>  travel_videos  = travelVideoService.queryByAttsId(attsId,userId);
                if (travel_videos != null && travel_videos.size()>0){
                    for(Travel_video_list travel_video_list:travel_videos){
                        Travel_attractions_line_info travel_attractions_line_info = get(travel_attractions,travel_video_list.getVideoAddress(),3);
                        list.add(travel_attractions_line_info);
                    }
                }
            }
            return list;
        }

        return null;
    }
    private Travel_attractions_line_info get(Travel_attractions travel_attractions,String address,int type){
        Travel_attractions_line_info travel_attractions_line_info = new Travel_attractions_line_info();
        String filePath = ipAddress+":"+serverPort+"/file/review?fileName="+UrlUtils.URLEncoder(address);
//        filePath = "E:\\ringing.wav";
        travel_attractions_line_info.setAddress(filePath);
        travel_attractions_line_info.setType(type);
        travel_attractions_line_info.setAttsId(travel_attractions.getAttsId());
        travel_attractions_line_info.setAttsName(travel_attractions.getAttsName());
        travel_attractions_line_info.setContent(travel_attractions.getContent());
        return travel_attractions_line_info;
    }
}
